package com.literaryfamily.literaryfamily.exceptions;

import com.literaryfamily.literaryfamily.model.ReturnResult;
import com.literaryfamily.literaryfamily.myUtil.LogUtil;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 错误描述信息,横坐标:等级errorLevel,纵坐标:错误代码errorNun
 * <table>
 *     <tr>
 *         <td></td>
 *     </tr>
 * </table>
 *
 * @author 滑稽/因果报应
 */
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {
    /**
     * 处理操作异常
     */
    @ExceptionHandler(OperationException.class)
    public ReturnResult<Exception> handleOperationException(OperationException ex) {
        return ReturnResult.isFalse(ex.getMsg());
    }

    @ExceptionHandler(Exception.class)
    public ReturnResult<Exception> handleException(Exception ex, Model model) {
        //操作错误时,获取错误信息返回给前端
        if (ex instanceof OperationException)
            return handleOperationException((OperationException) ex);
        ex.printStackTrace();//仅在开发时使用,上线时清除
        LogUtil.error(getClass(), ex);
        return ReturnResult.isError(ex.getMessage(), ex);
    }
}